<template>
	<view class="shop-coupon">
		<view class="list-item fill-base mg-big pd-lg radius-34" v-for="(item,index) in list.data" :key="index">
			<view class="flex-between">
				<view class="flex-center flex-column">
					<view class="flex-y-baseline f-caption c-warning">¥<view class="f-large-title">{{item.discount}}
						</view>
					</view>
					<view class="f-caption c-title">优惠券</view>
				</view>
				<view class="flex-1 ml-lg flex-between">
					<view class="f-title c-title mr-lg">
						<view class="text-bold">{{item.title}}</view>
						<view class="f-icontext mt-sm">{{item.type==0?`满${item.full}元可用`:'无门槛'}}</view>
						<view @tap.stop="this.list.data[index].is_show = !this.list.data[index].is_show"
							class="flex-y-baseline f-icontext mt-sm">查看详情
							<i class="iconfont ml-sm" style="font-size: 24rpx;"
								:class="[{'icon-down':!item.is_show},{'icon-up':item.is_show}]"></i>
						</view>
					</view>
					<auth :needAuth="userInfo &&  (!userInfo.nickName || !userInfo.phone)"
						:type="!userInfo.nickName?'userInfo':'phone'"
						:content="!userInfo.nickName ? '': configInfo.member_default_level ? `尊贵的用户，成为${configInfo.member_default_level}需要获取授权，授权成功后可领取相关卡券优惠` : ''"
						:must="!userInfo.nickName ? true : false" @go="toGetCoupon(index)" class="use-btn">
						<view class="use-btn flex-center f-caption c-base radius" :style="{background:primaryColor}">
							领取
						</view>
					</auth>
				</view>
			</view>
			<view class="mt-lg pd-lg f-icontext c-title radius-16" style="background: #F9FAF9" v-if="item.is_show">
				<view class="flex-warp">
					<view>使用时间：</view>
					<view class="flex-1 c-desc">{{item.time_limit == 2 ? `自领取后${item.day}天内可用` : item.start_time}}
					</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用范围：</view>
					<view class="flex-1 c-desc">{{item.use_range}}</view>
				</view>
				<view class="flex-warp mt-md">
					<view>使用规则：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.rule}}</text>
					</view>
				</view>
				<view class="flex-warp mt-md">
					<view>优惠详情：</view>
					<view class="flex-1 c-desc">
						<text decode="emsp" style="word-break:break-all;">{{item.text}}</text>
					</view>
				</view>
			</view>
		</view>

		<load-more :noMore="list.current_page>=list.last_page&&list.data.length>0" :loading="loading" v-if="loading">
		</load-more>
		<abnor type="COUPON" v-if="!loading&&list.data.length<=0&&list.current_page==1"></abnor>

		<view class="space-footer"></view>

		<uni-popup ref="show_coupon_succ_item" type="center" :custom="true" :maskClick="false">
			<view style="height: 100rpx;"></view>
			<view class="popup-coupon fill-base flex-center flex-column pt-lg radius-24">
				<image class="success-img" src="https://lbqny.migugu.com/admin/farm/success.png"></image>
				<view class="f-title c-title text-bold">恭喜您,领取成功</view>
				<!-- <view class="f-paragraph c-caption mt-lg">请在个人中心-我的卡券查看</view> -->
				<view @tap.stop="$refs.show_coupon_succ_item.close()"
					class="confirm-btn flex-center f-paragraph c-base mt-lg radius" :style="{background: primaryColor}">
					确定
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import {
		mapState,
	} from "vuex"
	export default {
		components: {},
		data() {
			return {
				options: {},
				useType: {
					is_land: '土地租赁',
					is_claim: '认养服务',
					is_shop: '商城',
				},
				param: {
					page: 1,
				},
				list: {
					data: []
				},
				loading: true,
				lockTap: false
			}
		},
		computed: mapState({
			primaryColor: state => state.config.configInfo.primaryColor,
			subColor: state => state.config.configInfo.subColor,
			configInfo: state => state.config.configInfo,
			userInfo: state => state.user.userInfo,
		}),
		onLoad(options) {
			this.$util.showLoading()
			this.options = options
			this.initIndex()
		},
		onUnload() {
			this.$util.back()
		},
		onPullDownRefresh() {
			this.param.page = 1;
			// #ifndef APP-PLUS
			uni.showNavigationBarLoading()
			// #endif
			this.initIndex();
			uni.stopPullDownRefresh()
		},
		onReachBottom() {
			if (this.list.current_page >= this.list.last_page || this.loading) return;
			this.param.page = this.param.page + 1;
			this.loading = true;
			this.getList();
		},
		methods: {
			initIndex() {
				this.param.page = 1
				this.getList()
			},
			initRefresh() {
				this.initIndex()
			},
			async getList() {
				let {
					list: oldList,
					param,
					useType
				} = this
				let newList = await this.$api.shop.couponList(param);
				newList.data.map(item => {
					item.is_show = false
					let use_range = ''
					for (let key in useType) {
						if (item[key]) {
							use_range += `${useType[key]}、`
						}
					}
					item.use_range = use_range.substring(0, use_range.length - 1)
				})
				//page=1为刷新或者首次加载,其他为下拉加载更多
				if (this.param.page == 1) {
					this.list = newList;
				} else {
					newList.data = oldList.data.concat(newList.data)
					this.list = newList;
				}
				this.loading = false;
				this.$util.hideAll();
			},
			async toGetCoupon(index) {
				let {
					id: coupon_id
				} = this.list.data[index]
				if (this.lockTap) return
				this.lockTap = true
				this.$util.showLoading()
				try {
					await this.$api.shop.userGetCoupon({
						coupon_id
					})
					let {
						coupon_total = 0
					} = this.$util.getPage(-1).detail
					this.$util.getPage(-1).detail.coupon_total = coupon_total - 1
					this.list.data.splice(index, 1)
					this.$util.hideAll()
					this.lockTap = false
					this.$refs.show_coupon_succ_item.open()
				} catch (e) {
					setTimeout(() => {
						this.lockTap = false
						this.$util.hideAll()
					}, 2000)
				}
			},
			goCoupon() {
				let url = `/mine/pages/coupon/list`
				this.$util.goUrl({
					url
				})
				this.$refs.show_coupon_succ_item.close()
			},
		}
	}
</script>


<style lang="scss">
	.shop-coupon {
		.list-item {
			overflow: hidden;

			.item-title {
				top: 0;
				left: 0;
				height: 50rpx;
				background: #FFE2E2;
				border-radius: 15rpx 0 15rpx 0;
			}

			.use-btn {
				min-width: 120rpx;
				height: 58rpx;
			}
		}

		.popup-coupon {
			width: 600rpx;
			height: 514rpx;

			.success-img {
				width: 201rpx;
				height: 199rpx;
			}

			.confirm-btn {
				width: 184rpx;
				height: 70rpx;
			}
		}
	}
</style>
